/*
 * @lc app=leetcode.cn id=61 lang=javascript
 *
 * [61] 旋转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
var rotateRight = function(head, k) {
  if(!head) return null;
  let pre = head, cur = pre, size = 1;
  while(cur.next){
    cur = cur.next;
    size++;
  }
  cur.next = head;

  let num = size - k%size -1;
  while(num--) {
    pre = pre.next;
  }
  cur = pre.next;
  pre.next = null;
  return cur;
};


/**
 * 复习 2021-08-04
 */
var rotateRight = function(head, k) {
  if(!head) return null;
  let tail = head;
  let len = 1;
  while(tail.next){
    tail = tail.next;
    len++;
  }
  tail.next = head;
  let pre = head, ret = head, n = len - (k % len);
  while(--n) {
    pre = pre.next;
  }
  ret = pre.next;
  pre.next = null;
  return ret;
}
// @lc code=end

